using UnityEngine;
using UnityEngine.SceneManagement;

namespace Runtime.Common.Utility
{
    public class SceneObjcet
    {
        public SceneObjcet(Scene curScene)
        {
            this.curScene = curScene;
            setRootObject();
        }

        private GameObject[] rootObjs;
        private Scene curScene;
        private Scene CurScene => curScene;

        private void setRootObject()
        {
            if (curScene != null)
            {
                rootObjs = curScene.GetRootGameObjects();
            }
        }

        public T GetComponentInAll<T>(bool includeInactive = false)
        {
            T temp = default;
            foreach (var obj in rootObjs)
            {
                temp = obj.GetComponentInChildren<T>(includeInactive);
                if (temp != null)
                {
                    break;
                }
            }

            return temp;
        }

        public static T GetComponentInAll<T>(Scene scene, out GameObject[] rootObjs, bool includeInactive = false)
        {
            rootObjs = new GameObject[0];
            if (scene != null)
            {
                rootObjs = scene.GetRootGameObjects();
            }

            T temp = default;
            foreach (var obj in rootObjs)
            {
                temp = obj.GetComponentInChildren<T>(includeInactive);
                if (temp != null)
                {
                    break;
                }
            }

            return temp;
        }
    }
}